﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BookMarkSendMessage.cs" company="Microsoft">
//   Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Activities;
using System.ComponentModel;
using SLS.ExClassLib.Message;
using SLS.ExClassLib.MVVM;
using SLS.ExClassLib.WCFClient;
using SLS.ExClassLib.Events;
using SLS.ExClassLib.RTIService;
namespace SLS.ExClassLib.WFActivities
{ 
    public sealed class BookMarkSendMessage : NativeActivity<ResultMessage>
    {
        #region Constants and Fields

        /// <summary>
        ///   The CardReader callback.
        /// </summary>
        private BookmarkCallback cardReaderCallback;

        #endregion

        #region Properties
        [DefaultValue("NO")]
        public InArgument<string> JobID { get; set; }
        [DefaultValue("NO")]
        public InArgument<string> JobContent { get; set; }
        [DefaultValue("NO")]
        public InArgument<string> Tag { get; set; }
        [DefaultValue("WF2UI")]
        public InArgument<string> MessageType { get; set; }
        [DefaultValue("NO")]
        public InArgument<string> UpdateFedTime { get; set; }
        /// <summary>
        ///   Gets or sets MachineEvent.
        /// </summary>
        public MachineEvent MachineEvent { get; set; }
        /// <summary>
        ///   Gets a value indicating whether CanInduceIdle.
        /// </summary>
        protected override bool CanInduceIdle
        {
            get
            {
                return true;
            }
        }

        /// <summary>
        ///   Gets CardReaderCallback.
        /// </summary>
        private BookmarkCallback CardReaderCallback
        {
            get
            {
                return this.cardReaderCallback ??
                       (this.cardReaderCallback = new BookmarkCallback(this.OnCardReaderCallback));
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// The cache metadata.
        /// </summary>
        /// <param name="metadata">
        /// The metadata.
        /// </param>
        protected override void CacheMetadata(NativeActivityMetadata metadata)
        {
            metadata.RequireExtension(typeof(IDataModel));
            metadata.RequireExtension(typeof(WFMachine));
            metadata.AddArgument(new RuntimeArgument("JobID", typeof(string), ArgumentDirection.In, false));
            metadata.AddArgument(new RuntimeArgument("JobContent", typeof(string), ArgumentDirection.In, false));
            metadata.AddArgument(new RuntimeArgument("MessageType", typeof(string), ArgumentDirection.In, false));
            metadata.AddArgument(new RuntimeArgument("Tag", typeof(string), ArgumentDirection.In, false));
            metadata.AddArgument(new RuntimeArgument("UpdateFedTime", typeof(string), ArgumentDirection.In, false));
        }

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="context">
        /// The context.
        /// </param>
        protected override void Execute(NativeActivityContext context)
        {
            try
            {
                // MachineEvent.None is a no op
                var wfvw = context.GetExtension<IDataModel>();
                var wfm = context.GetExtension<WFMachine>();
                if (this.MachineEvent != MachineEvent.None)
                {
                    context.CreateBookmark(this.MachineEvent.ToString(), this.CardReaderCallback);
                    MessageEx msg = new MessageEx(wfm.HostAddress, "WFFederation", this.JobID.Get(context), this.UpdateFedTime.Get(context));
                    msg.JobType = this.MessageType.Get(context);
                    msg.JobContent = this.JobContent.Get(context);
                    if (this.Tag != null)
                    {
                        msg.Tag = this.Tag.Get(context);
                    }
                    msg.TimeStamp = ContactRTIService.Instance.GetRTITimeStamp(msg);
                    wfvw.SendMessage(msg);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        /// <summary>
        /// The on CardReader callback.
        /// </summary>
        /// <param name="context">
        /// The context.
        /// </param>
        /// <param name="bookmark">
        /// The bookmark.
        /// </param>
        /// <param name="value">
        /// The value.
        /// </param>
        /// <exception cref="InvalidOperationException">
        /// </exception>
        private void OnCardReaderCallback(NativeActivityContext context, Bookmark bookmark, object value)
        {
            if (value is ResultMessage)
            {
                this.Result.Set(context, value as ResultMessage);
            }
            else if (value != null)
            {
                // Resumed with something else
                throw new InvalidOperationException(
                    "You must resume the CardReader bookmark with a result of type ResultMessage");
            }
        }

        #endregion
    }
}